<template>
    <div class="home">
      <!-- 头部导航 -->
      <header class="home-header wrap" :class="{'active' : headerScroll}">
        <router-link tag="i" to="./category"><i class="iconfont iconmenu"></i></router-link>
        <div class="header-search">
          <span class="app-name">识货商城</span>
          <i class="iconfont iconfenge"></i>
          <router-link tag="span" class="search-title" to="product-list">运动潮流好物</router-link>
        </div>
        <router-link tag="span" class="login" to="./login" v-if="!isLogin">登录</router-link>
        <router-link class="login" tag="span" to="./user" v-else>
          <van-icon name="manager-o"></van-icon>
        </router-link>
      </header>
      <nav-bar></nav-bar>
      <!-- 轮播图 -->
      <swiper :list="swiperList"></swiper>
      <!-- 中部分类栏目 -->
      <div class="category-list">
        <div v-for="item in categoryList" :key="item.categoryId">
          <img :src="item.imgUrl">
          <span>{{item.name}}</span>
        </div>
      </div>
      <!-- 商品推荐栏目 -->
      <!-- 新品上线 -->
      <div class="good">
        <header class="good-header">新品上线</header>
        <div class="good-box">
          <div class="good-item" v-for="item in newGoodses" :key="item.goodsId" @click="goToDetail(item)">
            <img :src="`//lmall.xinfeng.site${item.goodsCoverImg}`" alt="">
            <div class="good-desc">
              <div class="title">{{item.goodName}}</div>
              <div class="price">¥ {{ item.sellingPrice }}</div>
            </div>
          </div>
        </div>
      </div>
      <!-- 热门商品 -->
      <div class="good">
        <header class="good-header">热门商品</header>
        <div class="good-box">
          <div class="good-item" v-for="item in hots" :key="item.goodsId" @click="goToDetail(item)">
            <img :src="`//lmall.xinfeng.site${item.goodsCoverImg}`" alt="">
            <div class="good-desc">
              <div class="title">{{item.goodName}}</div>
              <div class="price">¥ {{ item.sellingPrice }}</div>
            </div>
          </div>
        </div>
      </div>
      <!-- 最新推荐 -->
      <div class="good">
        <header class="good-header">最新推荐</header>
        <div class="good-box">
          <div class="good-item" v-for="item in recommends" :key="item.goodsId" @click="goToDetail(item)">
            <img :src="`//lmall.xinfeng.site${item.goodsCoverImg}`" alt="">
            <div class="good-desc">
              <div class="title">{{item.goodName}}</div>
              <div class="price">¥ {{ item.sellingPrice }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
</template>

<script>
import navBar from '@/components/NavBar'
import { getLocal } from '@/common/js/utils'
import swiper from '@/components/Swiper'
import { getHome } from '../service/home'
import { Toast } from 'vant'
export default {
  name:"home",
  data(){
    return{
      isLogin: false,
      swiperList:[],
      hots:[],
      newGoodses:[],
      recommends:[],
      headerScroll:false,
      categoryList:[
        {
            name: '识货超市',
            imgUrl: '//s.weituibao.com/1583585285461/cs.png',
            categoryId: 100001
          }, {
            name: '潮流服饰',
            imgUrl: '//s.weituibao.com/1583585285468/fs.png',
            categoryId: 100003
          }, {
            name: '全球购',
            imgUrl: '//s.weituibao.com/1583585285470/qq.png',
            categoryId: 100002
          }, {
            name: '识货生鲜',
            imgUrl: '//s.weituibao.com/1583585285472/sx.png',
            categoryId: 100004
          }, {
            name: '好物到家',
            imgUrl: '//s.weituibao.com/1583585285467/dj.png',
            categoryId: 100005
          }, {
            name: '充值缴费',
            imgUrl: '//s.weituibao.com/1583585285465/cz.png',
            categoryId: 100006
          }, {
            name: '9.9元拼',
            imgUrl: '//s.weituibao.com/1583585285469/pt.png',
            categoryId: 100007
          }, {
            name: '领劵',
            imgUrl: '//s.weituibao.com/1583585285468/juan.png',
            categoryId: 100008
          }, {
            name: '省钱',
            imgUrl: '//s.weituibao.com/1583585285471/sq.png',
            categoryId: 100009
          }, {
            name: '全部',
            imgUrl: '//s.weituibao.com/1583585285470/qb.png',
            categoryId: 100010
          }
      ],
    }
  },
  components:{
    swiper,
    navBar
  },
  async mounted(){  
    window.addEventListener('scroll',this.pageScroll)
    const token = getLocal('token')
    if(token){
      this.isLogin = true
    }
    Toast.loading({
      message: '加载中...',
      forbidClick: true
    });
    const { data } = await getHome()
    this.swiperList = data.carousels
    this.newGoodses = data.newGoodses // 新品上线
    this.hots = data.hotGoodses // 热门商品
    this.recommends = data.recommendGoodses // 最新推荐
    Toast.clear() // 数据请求结束，清楚弹窗
  },

  methods:{
    pageScroll(){
       let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      scrollTop > 100 ? this.headerScroll = true : this.headerScroll = false
    },
    goToDetail(item) {
      this.$router.push({ path: `product/${item.goodsId}` })
    }
  }
}
</script>

<style lang="less" scoped>
@import '../common/style/mixin.less';
.home{
  .home-header{
    position: fixed;
    left:0;
    top:0;
    .wh(100%,50px);
    .fj();
    line-height: 50px;
    padding: 0 15px;
    .boxSizing();
    font-size: 15px;
    color:#000;
    z-index:10000;
    .iconfenge{
      color: @primary;
    }
    &.active{
      background: @primary;
      .iconmenu{
        color: #fff;
      }
      .login{
        color: #fff;
      }
    }

    .header-search{
      display: flex;
      .wh(74%, 20px);
      line-height: 20px;
      margin: 10px 0;
      padding: 5px 0;
      color: #232326;
      background: #ffffff;
      border-radius: 20px;
      .app-name{
        padding: 0 10px;
        color: @primary;
        font-size:20px;
        margin-left: 15px;
        font-weight: bold;
        border-right: 1px soild #666
      }
      .iconfenge{
        padding: 0 5px;
        font-size:17px;
      }
      .search-title{
        font-size: 12px;
        color: #666;
        line-height: 21px;
      }
    }
    .login{
      color:@primary;
      line-height: 52px;
      .van-icon-manager-o {
          font-size: 20px;
          vertical-align: -3px;
        }
    }
  }
  .category-list {
      display: flex;
      flex-shrink: 0;
      flex-wrap: wrap;
      width: 100%;
      padding-bottom: 13px;
      div {
        display: flex;
        flex-direction: column;
        width: 20%;
        text-align: center;
        img {
          .wh(40px, 40px);
          margin: 13px auto 8px auto;
        }
      }
  }
  .good{
    .good-header{
      background: #f9f9f9;
      height: 50px;
      line-height: 50px;
      text-align: center;
      color: @primary;
      font-size:16px;
      font-weight: bold;
    }
    .good-box{
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      .good-item{
        box-sizing:border-box;
        width: 50%;
        border-bottom: 1px solid #e9e9e9;
        padding : 10px 10px;
        img{
          display: block;
          width:120px;
          margin: 0 auto;
        }
        .good-desc{
          text-align: center;
          font-size: 14px;
          padding: 10px 0;
          .title{
            color:#222333;
          }
          .price{
            color:#222333;
          }
        }
        &:nth-child(2n + 1) {
          border-right: 1PX solid #e9e9e9;
        }
      }


    }
  }
}
</style>